//
//  BLDeviceRotation.m
//  yiplayer
//
//  Created by cgw on 2024/6/26.
//

#import "BLDeviceRotation.h"

@implementation BLDeviceRotation

static BLDeviceRotation * dev =nil ;
+(BLDeviceRotation *)share{
    static dispatch_once_t predicate;
    //static ServiceManager * sharedManager =nil ;
    dispatch_once(&predicate, ^{
        dev = [[BLDeviceRotation alloc] init];
    });
    return dev;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.currOri = UIInterfaceOrientationUnknown;
        [self addOrientationNoti];
    }
    return self;
}

///强制转换屏幕方向
+ (void)interfaceOrientation:(UIInterfaceOrientation)orientation{
    [[self share] interfaceOrientation:orientation];
}

+ (UIInterfaceOrientation)currOrientation{
    return [self share].currOri;
}

#pragma mark - Private
//设备方向改变通知
- (void)addOrientationNoti {
    //注册通知
//            if !UIDevice.current.isGeneratingDeviceOrientationNotifications {
//                //生成通知
//                UIDevice.current.beginGeneratingDeviceOrientationNotifications()
//            }
//    NotificationCenter.default.addObserver(self, selector: #selector(handleDeviceOrientationChange(notification:)), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
    
    [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
}

- (void)orientationChanged:(NSNotification*)noti{
    UIDeviceOrientation ori = UIDevice.currentDevice.orientation;
    if( ori == UIDeviceOrientationPortrait) {
        self.currOri = UIInterfaceOrientationPortrait;
    }else if ( ori == UIDeviceOrientationLandscapeLeft ) {
        self.currOri = UIInterfaceOrientationLandscapeLeft;
    }else if ( ori == UIDeviceOrientationLandscapeRight ) {
        self.currOri = UIInterfaceOrientationLandscapeRight;
    }else if ( ori == UIDeviceOrientationPortraitUpsideDown) {
        self.currOri = UIInterfaceOrientationPortraitUpsideDown;
    }
}

- (void)interfaceOrientation:(UIInterfaceOrientation)orientation {
    
//    BOOL isFullScreen = UIInterfaceOrientationIsLandscape(self.currOri);
//    BOOL isToFullScreen = UIInterfaceOrientationIsLandscape(orientation);
//    if (isFullScreen == isToFullScreen) {
//        return;
//    }
    
    
    if(@available(iOS 16.0, *)) {
        UIScene *windowScene = [[[UIApplication sharedApplication] connectedScenes] anyObject];
        UIWindowSceneGeometryPreferencesIOS *preferences = nil;
        if (orientation == UIInterfaceOrientationLandscapeRight || (orientation == UIInterfaceOrientationLandscapeLeft)) {
            preferences = [[UIWindowSceneGeometryPreferencesIOS alloc] initWithInterfaceOrientations:UIInterfaceOrientationMaskLandscape];
        }else {
            preferences = [[UIWindowSceneGeometryPreferencesIOS alloc] initWithInterfaceOrientations:UIInterfaceOrientationMaskPortrait];
        }
        [(UIWindowScene *)windowScene requestGeometryUpdateWithPreferences:preferences errorHandler:^(NSError * _Nonnull error) {
        }];

    }else {
        if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
    
            SEL selector = NSSelectorFromString(@"setOrientation:");
            NSInvocation * invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
            [invocation setSelector:selector];
            [invocation setTarget:[UIDevice currentDevice]];
            int val = orientation;
            [invocation setArgument:&val atIndex:2];
            [invocation invoke];
        }
    }
    
    self.currOri = orientation;
}


@end
